package cn.cocho.android.mc.async;

import android.app.Activity;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;

import cn.cocho.android.mc.comp.dialog.McProgressBar;

public class ProgressAsyncTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {

	McProgressBar progressBar;
	private CancelBtnListener cancelListener = null;
	
	public ProgressAsyncTask(Activity activity){
		init(activity);
	}
	
	public ProgressAsyncTask(Activity activity, CharSequence message){
		init(activity);
		progressBar.setMessage(message);
	}
	
	private void init(Activity activity){
		progressBar = new McProgressBar(activity);
		progressBar.setCancelable(false);
		progressBar.setCancelBtnListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				cancel(true);
				progressBar.dismiss();
				if(cancelListener!=null){
					cancelListener.clickCancelBtn();
				}
			}
		});
	}
	
	@Override
	protected Result doInBackground(Params... arg0) {
		return null;
	}
	
	@Override
	protected void onPostExecute(Result result) {
		progressBar.dismiss();
		
	}
	
	public CancelBtnListener getCancelListener() {
		return cancelListener;
	}

	public void setCancelListener(CancelBtnListener cancelListener) {
		this.cancelListener = cancelListener;
	}
	
	public interface CancelBtnListener{
		public void clickCancelBtn();
	}

}
